package com.phone580.app.apphome;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.phone580.app.BaseActivity;
import com.phone580.app.R;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

public class AppOnSDCardActivity extends BaseActivity {
	private ListView lvSDCard;
	private AppOnSDCardAdapter adapter;
	private List<AppInfoEntity> data;
	private ProgressBar pbLoad;
	public static AppOnSDCardActivity appsd;
	
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			adapter.setData(data);
			lvSDCard.setAdapter(adapter);
			pbLoad.setVisibility(View.GONE);
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_sdcard_layout);

		lvSDCard = (ListView) findViewById(R.id.app_sdcard_list);
		adapter = new AppOnSDCardAdapter(this);
		pbLoad = (ProgressBar) findViewById(R.id.app_sdcard_load_pb);
		appsd = this;
//		refresh();
	}
	
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//		refresh();
//		if (AppOnPhoneActivity.appPhone != null) {
//			AppOnPhoneActivity.appPhone.refresh();
//		}
//	}
	
	@Override
	protected void onResume() {
		super.onResume();
		refresh();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appsd = null;
	}
	
	public void refresh() {
		AppHomeActivity.instance.updateTitleDisplaySize();
		Thread t = new WorkerThread();
		adapter.setData(new ArrayList<AppInfoEntity>());
		adapter.notifyDataSetChanged();
		pbLoad.setVisibility(View.VISIBLE);
		t.start();
	}
	
	class WorkerThread extends Thread {
		 @Override
		public void run() {
//			 List<PackageInfo> apps = new ArrayList<PackageInfo>();
//				PackageManager pManager = getPackageManager();
//				// 获取手机内所有应用
//				List<PackageInfo> paklist = pManager.getInstalledPackages(0);
//				for (int i = 0; i < paklist.size(); i++) {
//					PackageInfo pak = (PackageInfo) paklist.get(i);
//					// 判断是否为非系统预装的应用程序
//					if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
//						// customs applications
//						apps.add(pak);
//					}
//				}
//
//				data = new ArrayList<AppInfoEntity>();
//				for (PackageInfo pkg : apps) {
//					String label = pManager.getApplicationLabel(pkg.applicationInfo)
//							.toString();
//					String pkgName = pkg.packageName;
//					String version = pkg.versionName;
//					Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
//					String srcPath = pkg.applicationInfo.sourceDir;
//					File file = new File(srcPath);
//					long size = file.length();
//					AppInfoEntity info = new AppInfoEntity();
//					info.setAppLabel(label);
//					info.setIcon(icon);
//					info.setPkgName(pkgName);
//					info.setVersion(version);
//					info.setSize(size);
//					//判断安装位置
//					if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
//						info.setInstallAtSDCard(true);
//						data.add(info);
//					} else {
//						info.setInstallAtSDCard(false);
//						continue;
//					}
//				}
			data = CommonUtility.getAppOnSDCardList(AppOnSDCardActivity.this);
			handler.sendEmptyMessage(0);
		}
	}
}
